package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.zkoss.xel.fn.StringFns;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zul.A;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.lt.flashcard.model.entity.Role;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.UserService;

public class AdminUsersListCtrl  extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
//	String username;
//	String emailname;
	private User user;
	
	public List<User> getLstUser() {
		Component com = Path.getComponent("//admin_user_page/ctnAdminUserpage");
		AdminUsersPageCtrl ctrl = (AdminUsersPageCtrl) com.getAttribute("$composer");
		return ctrl.getUserList();
	}
	
	public String getUserRole(User u) {
//		Component com = Path.getComponent("//admin_user_page/ctnAdminUserpage");
//		AdminUsersPageCtrl ctrl = (AdminUsersPageCtrl) com.getAttribute("$composer");
		List<String> role = new ArrayList<String>();
		for(Role r: u.getRoles()){
			role.add(r.getName());
		}
		return StringFns.join(role.toArray(), ", ");
	}
	
//	private String getUserName(){
//		Component com = Path.getComponent("//admin_user_page/ctnAdminUserpage");
//		AdminUsersPageCtrl ctrl = (AdminUsersPageCtrl) com.getAttribute("$composer");
//		return ctrl.getUsername().getValue();
//	}
//	
//	private String getEmailName(){
//		Component com = Path.getComponent("//admin_user_page/ctnAdminUserpage");
//		AdminUsersPageCtrl ctrl = (AdminUsersPageCtrl) com.getAttribute("$composer");
//		return ctrl.getEmailname().getValue();
//	}
	
	@Listen("onClick=.btnDelete")
	public void doDelete(Event event) {
		Component com = Path.getComponent("//admin_user_page/ctnAdminUserpage");
		final AdminUsersPageCtrl ctrl = (AdminUsersPageCtrl) com.getAttribute("$composer");
		A a = (A) event.getTarget();
		final Long id = (Long) a.getAttribute("ctdelete");
		
		Messagebox.show("Are you sure to delete this user?","Confirm Dialog", Messagebox.OK | Messagebox.CANCEL, id.toString(), new org.zkoss.zk.ui.event.EventListener<Event>() {	
			@Override
            public void onEvent(Event event) throws Exception {
				if (event.getName().equals("onOK")) {
					UserService usrv = Repository.getInstance().getUserService();
					try{
						User u = usrv.findByID(id);
						usrv.delete(u);
//						username = getUserName();
//						emailname = getEmailName();
						ctrl.doFilter();
						ctrl.getCtnUserlist().invalidate();
						Messagebox.show("User had been deleted.", "Information", Messagebox.OK, Messagebox.INFORMATION);
					}
					catch (Exception e) {
						Messagebox.show(e.getMessage());
					}
		        } 
			}
		});
	}
	
	@Listen("onClick=.btnEdit")
	public void doEdit(Event event){
		A a = (A) event.getTarget();
		Long id = (Long) a.getAttribute("ctedit");
		UserService usrv = Repository.getInstance().getUserService();
		user = usrv.findByID(id);
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("user", user);
		Component comp = Executions.createComponents("/create_edit_user.zul", null, param);
		AdminCreateUsersCtrl ctrl = (AdminCreateUsersCtrl) comp.getAttribute("$composer");
		Set<Role> rs = user.getRoles();
		for(Checkbox chk : ctrl.getChkRole()) {
			for(Role r : rs){
				if(chk.getLabel().equals(r.getName())){
					chk.setChecked(true);
				}
			}
		}
		((Window) comp).doModal();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}